<template>
  <div class="home-container">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">商品管理</a></el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">商品列表</a></el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-form
        ref="goodsFormref"
        :model="queryInfo"
        label-width="90px"
        class="form"
      >
        <el-row type="flex" justify="space-between">
          <el-col :span="6">
            <el-form-item label="商品名称" prop="goodName">
              <el-input v-model="queryInfo.goodName"></el-input>
            </el-form-item>
            <el-form-item label="品牌名称" prop="brand">
              <el-select v-model="queryInfo.brand" placeholder="请选择">
                <el-option
                  v-for="item in brands"
                  :key="item.id"
                  :label="item.brandName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6" class="btn-box">
            <el-button @click="resetAddForm('goodsFormref')">重置</el-button>
            <el-button @click="addGood()">新增</el-button>
            <el-button type="primary" class="btn-blue" @click="getGoodsList()"
              >查询</el-button
            >
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-table
        :data="goodsList"
        style="width: 100%; margin-top: 20px"
        border
        stripe
      >
      <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="商品名称">
                <span>{{ props.row.goodName }}</span>
              </el-form-item>
              <el-form-item label="所属店铺">
                <span>{{ props.row.shopName }}</span>
              </el-form-item>
              <el-form-item label="商品分类">
                <span>{{ props.row.category }}</span>
              </el-form-item>
              <el-form-item label="店铺地址">
                <span>{{ props.row.shopLocation }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column type="index" label="序号" :index="indexMethod" width="50"> </el-table-column>
        <el-table-column prop="goodName" label="商品名称"> </el-table-column>
        <el-table-column prop="model" label="商品型号"> </el-table-column>
        <el-table-column prop="brand" label="品牌名称"> </el-table-column>
        <el-table-column prop="price" label="单价"> </el-table-column>
        <el-table-column prop="number" label="库存"> </el-table-column>
        <el-table-column fixed="right" label="操作" width="200">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              class="btn-blue"
              @click="open_editForm(scope.row.id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="confirm_del(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog title="编辑商品" :visible.sync="editVisible" width="50%">
      <!-- 内容主体区域 -->
      <el-form label-width="80px" ref="editFormref" :model="editForm">
        <div class="public-title-1">商品基本信息</div>
        <el-form-item label="商品名称" prop="goodName">
          <el-input v-model="editForm.goodName"></el-input>
        </el-form-item>
        <el-form-item label="商品类别" prop="category">
          <el-input autocomplete="off" v-model="editForm.category"></el-input>
        </el-form-item>
        <el-form-item label="商品库存" prop="number">
          <el-input-number v-model="editForm.number" :min="1"></el-input-number>
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input autocomplete="off" v-model="editForm.unit"></el-input>
        </el-form-item>
        <el-form-item label="品牌名称" prop="brand">
          <el-select v-model="editForm.brand" placeholder="请选择">
            <el-option
              v-for="item in brands"
              :key="item.id"
              :label="item.brandName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <div class="public-title-1">商品图片</div>
        <div class="img-container">
          <viewer :images="fileList">
          <img
            v-for="(src, index) in fileList"
            :src="`http://127.0.0.1:3007/${src}`"
            :key="index"
            class="img"
          />
        </viewer>
        <el-upload
          action="http://127.0.0.1:3007/goods/uploadImg"
          :before-upload="beforeAvatarUpload"
          :show-file-list="false"
          list-type="picture-card"
          :on-success="handleSuccess"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
        </div>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="submitEditForm('editFormref')"
          class="btn-blue"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data () {
    return {
      goodsList: [],
      queryInfo: {
        goodName: '',
        brand: '',
        limit: 10,
        page: 1
      },
      brands: [],
      editForm: {
        fileId: '',
        goodName: '',
        category: '',
        brand: '',
        number: 0,
        unit: '',
        url: '',
        shopName: '',
        model: '',
        id: 0
      },
      editVisible: false,
      fileList: [],
      fileIdList: [],
      dialogImageUrl: '',
      dialogVisible: false
      // headers: {
      //   'Content-Type': 'multipart/form-data'
      // }
    }
  },
  created () {
    this.getGoodsList()
    this.getBrands()
  },
  computed: {},
  methods: {
    async getGoodsList () {
      const { data: res } = await this.$http.post('/goods/all', this.queryInfo)
      if (res.code !== 200) {
        return this.$message.error(res.msg)
      }
      this.goodsList = res.data
    },
    async getBrands () {
      const { data: res } = await this.$http.get('/goods/brand')
      if (res.code !== 200) {
        return this.$message.error(res.msg)
      }
      this.brands = res.data
    },
    async getGoodsDetail (id) {
      const { data: res } = await this.$http.post('/goods/search', {
        id: id
      })
      if (res.code !== 200) {
        return this.$message.error(res.msg)
      }
      this.editForm = res.data

      for (let i = 0; i < res.data.url.length; i++) {
        this.fileList.push(res.data.url[i].picName)
      }
      // this.fileList = res.data.url !== undefined ? res.data.url.split(',') : []
      this.fileIdList = res.data.id !== undefined ? res.data.fileId.split(',') : []
    },
    open_editForm (id) {
      this.$router.push(`/addGoods?id=${id}`)
      // this.editVisible = true
      // 重置图片列表
      // this.fileList = []
      // this.getGoodsDetail(id)
    },
    confirm_del (id) {
      this.$confirm('是否确认删除商品信息', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await this.$http.post('/goods/delete', {
            id: id
          })
          if (res.code !== 200) {
            return this.$message.error(res.msg)
          }
          this.$message.success('删除成功！')
          this.getGoodsList()
        })
        .catch(() => {})
    },
    addGood () {
      this.$router.push('/addGoods')
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg' || 'image/jpeg' || 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    async submitEditForm () {
      this.editForm.fileId = this.fileIdList.join(',')
      const { data: res } = await this.$http.post(
        '/goods/updateGoods',
        this.editForm
      )
      if (res.code !== 200) {
        this.editVisible = false
        return this.$message.error(res.msg)
      }
      this.$message.success('编辑成功')
      this.editVisible = false
      this.fileIdList = []
      this.fileList = []
      this.editForm = {}
      this.getGoodsList()
    },
    handleSuccess (res, file) {
      if (res.code !== 200) {
        return this.$alert.$message.error('上传图片失败！')
      }
      this.fileIdList.push(res.data.id)
      this.fileList.push(res.data.url)
    },
    indexMethod (index) {
      return (this.queryInfo.page - 1) * this.queryInfo.limit + index + 1
    },
    // 重置表单
    resetAddForm (formName) {
      this.$refs[formName].resetFields()
      this.getGoodsList()
    }
  }

}
</script>
<style lang="less" scoped>
.form {
  display: flex;

  .el-form-item {
    margin-bottom: 0px;
  }
}

.el-row {
  width: 100%;
}

.img-container{
    display:flex;
    .img{
      margin-right: 10px;
    }
  }
.el-col {
  width: fit-content;
  display: flex;
}
.img {
    width: 146px;
    height: 146px;
  }
  .demo-table-expand {
    font-size: 0;
    padding-left: 20px;
  }
  .demo-table-expand label {
    width: 90px;
    color: rgba(0, 0, 0, 0.65);
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }

</style>
